<?php

class Product extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    //后台列表页
    public function index()
    {
        $act = $this->input->get_post('ajaxdata');
        $result = array('code' => 201, 'message' => '', 'result' => '');

        switch ($act) {
            case 'addFav':
                $this->load->library('c_collection');
                $sendData = array();
                $sendData['product_id'] = $this->input->get_post('id');
                $sendData['type'] = $this->input->get_post('type');
                $sendData['password'] = $this->lg_user['password'];
                $sendData['uid'] = $this->lg_user['id'];
                $sendData['enterpriseId'] = $this->lg_user['enterpriseId'];
                $sendData['channelId'] = $this->lg_user['channelId'];
                $sendData['userType'] = $this->lg_user['type'];

                //制造接口请求
                $url = $this->c_collection->set_call_items($sendData);
                $param = $this->c_collection->get_call_items();
                $rs = $this->restcurl->put($url, $param);

                if ($rs['ErrorCode'] == 0 && $rs['Success'] == 1 && $rs['ErrorMsg'] == '') {
                    if ($sendData['type'] == 1) {
                        $result['code'] = 200;
                        $result['message'] = '收藏成功';
                    } elseif ($sendData['type'] == 0) {
                        $result['code'] = 200;
                        $result['message'] = '取消收藏';
                    }
                }
                break;
            case 'addCart':
                $gid = $this->input->get_post('id');
                $num = (int)$this->input->get_post('num');

                $cartData = json_decode(gCookie('user_cart'), true);
                if (is_array($cartData)) {
                    $cartData[$gid] = isset($cartData[$gid]) ? ($cartData[$gid] + $num) : $num;
                } else {
                    $cartData = array($gid => $num);
                }
                sCookie('user_cart', json_encode($cartData));
                $result['code'] = 200;
                $result['message'] = '加入购物车成功';
                break;
            default:
                break;
        }
        echo json_encode($result);
        die;
    }

}
